{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/sort-array-by-parity-ii/\n",
    "\n",
    "\n",
    "Runtime: 68 ms, faster than 78.49% of C online submissions for Sort Array By Parity II.\n",
    "Memory Usage: 14.4 MB, less than 5.38% of C online submissions for Sort Array By Parity II.\n",
    "\n",
    "\n",
    "\n",
    "```c\n",
    "#include <stdio.h>\n",
    "\n",
    "/**\n",
    " * Note: The returned array must be malloced, assume caller calls free().\n",
    " */\n",
    "int* sortArrayByParityII(int* A, int ASize, int* returnSize){\n",
    "    *returnSize = ASize;\n",
    "\n",
    "    int *EVEN = (int*) malloc(sizeof(int)*ASize);\n",
    "    int evenI = 0;\n",
    "    int *ODD = (int*) malloc(sizeof(int)*ASize);\n",
    "    int oddI = 0;\n",
    "\n",
    "    for(int i = 0; i<ASize; i++) {\n",
    "        int v = A[i];\n",
    "        if (v % 2 == 0) {\n",
    "            //even\n",
    "            EVEN[evenI] = v;\n",
    "            evenI += 1;\n",
    "        } else {\n",
    "            ODD[oddI] = v;\n",
    "            oddI += 1;\n",
    "        }\n",
    "    }\n",
    "    EVEN[evenI] = NULL;\n",
    "    ODD[oddI] = NULL;\n",
    "    oddI -= 1;\n",
    "    evenI -= 1;\n",
    "\n",
    "    int *r = (int *) malloc(sizeof(int)*ASize);\n",
    "    for(int i = 0; i<ASize; i++) {\n",
    "        if (i % 2 == 0) {\n",
    "            //even\n",
    "            r[i] = EVEN[evenI];\n",
    "            evenI -= 1;\n",
    "        } else {\n",
    "            r[i] = ODD[oddI];\n",
    "            oddI -= 1;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    free(EVEN);\n",
    "    free(ODD);\n",
    "\n",
    "    return r;\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
